using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Acms.Bll;

/// <summary>
/// Summary description for BasePostPage
/// </summary>
public abstract class BasePostPage : BasePage
{
    protected override void OnLoad(EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                postId = int.Parse(Request.QueryString["Id"]);
            }
            catch (FormatException)
            {
            }
            catch (ArgumentNullException)
            {
            }

            if (!string.IsNullOrEmpty(Request.QueryString["SV"]))
            {
                try
                {
                    SelectedVersion = DateTime.Parse(Request.QueryString["SV"]);
                }
                catch
                {
                }
            }

            if (CurrentPost != null)
            {
                this.Title = string.Format("{0} {1}", CurrentPost.DisplayName, this.Title);
            }
        }
        base.OnLoad(e);
    }

    private int postId;
    public int PostId
    {
        get { return postId; }
    }

    private Acms.Bll.Post currentPost;
    public Acms.Bll.Post CurrentPost
    {
        get
        {
            if (currentPost == null && postId > 0)
            {
                currentPost = Acms.Bll.Post.GetById(AcmsContext.SiteId, postId, SelectedVersion);
            }
            return currentPost;
        }
    }

    public DateTime? SelectedVersion { get; private set; }
    protected void SetSelectedVersion(DateTime? version)
    {
        SelectedVersion = version;
    }

    protected override object SaveViewState()
    {
        ViewState["PostId"] = postId;
        ViewState["SelectedVersion"] = SelectedVersion;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["PostId"] is int)
        {
            postId = (int)ViewState["PostId"];
        }
        SelectedVersion = ViewState["SelectedVersion"] as DateTime?;
    }

    public string AppendQueryStringParameters(string url, int postId, DateTime? selectedVersion)
    {
        StringBuilder tmp = new StringBuilder();
        if(url.Contains("&Id="))
        {
            tmp.Append(url.Substring(0, url.IndexOf("&Id=")));
        }
        else if(url.Contains("?Id="))
        {
            tmp.Append(url.Substring(0, url.IndexOf("?Id=")));
        }
        else
        {
            tmp.Append(url);
        }

        if (tmp.ToString().Contains("?"))
        {
            tmp.Append("&");
        }
        else
        {
            tmp.Append("?");
        }
        tmp.AppendFormat("Id={0}&SV={1}", postId, selectedVersion.HasValue ? Server.UrlEncode(selectedVersion.Value.ToString("yyyy/MM/dd HH:mm")) : "");
        return tmp.ToString();
    }

    public string AppendQueryStringParameters(string url)
    {
        return AppendQueryStringParameters(url, PostId, SelectedVersion);
    }

    protected void UpdatePostRequiesPublishing(Guid transactionKey)
    {
        if (CurrentPost != null && CurrentPost.PublishFrom == null && !CurrentPost.RequiresPublishing)
        {
            CurrentPost.RequiresPublishing = true;
            CurrentPost.UseTransaction(transactionKey);
            CurrentPost.Update();
            CurrentPost.ClearTransactionKey(transactionKey);
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        breadcrumbs.Add("CMS", "~/Posts");
        int tmpIndex = breadcrumbs.Count;
        if (CurrentPost != null)
        {
            breadcrumbs.Add(CurrentPost.DisplayName, string.Format("~/Posts/Details.aspx?Id={0}", PostId));
            var tmp = CurrentPost.Parent;
            while (tmp != null)
            {
                breadcrumbs.Insert(tmpIndex, new BaseMasterPage.BreadCrumb(tmp.DisplayName, string.Format("~/Posts/Details.aspx?Id={0}", tmp.Id)));
                tmp = tmp.Parent;
            }
        }
    }
}
